// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Tool from "../../framework/Tool";
import sceneMediator from "../../framework/sceneMediator";
import languageManager from "../languageManager/languageManager";
import appConfig from "../../framework/appConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class chooseLevelRank extends cc.Component {

    @property(cc.SpriteFrame)
    spriteList_0 : cc.SpriteFrame[] = [];

    @property(cc.SpriteFrame)
    spriteList_1 : cc.SpriteFrame[] = [];

    @property(cc.SpriteFrame)
    lockList : cc.SpriteFrame[] = [];

    @property(cc.SpriteFrame)
    lock : cc.SpriteFrame = null;

    @property(cc.Node)
    btnItem : cc.Node = null;

    labStringList = null;

    levelData = null;

    // onLoad () {}

    start () {

        this.labStringList = languageManager.getChooseLevelRankLevelString();
        this.levelData = appConfig.levelRankLayer;

        this.initLevelView(4);
    }

    /*
    @param num 初始化人物的个数
    */
    initLevelView(num){

        let content_0 = this.node.getChildByName("content_0");
        let content_1 = this.node.getChildByName("content_1");

        for(let i in content_0.children){
            this.addButtonEvent(content_0.children[i],10 + parseInt(i));
            let lab = content_0.children[i].getChildByName("lab");

            lab.getComponent(cc.Label).string = this.labStringList.layer_0[i];
            let pic = content_0.children[i].getChildByName("pic");
            pic.getComponent(cc.Sprite).spriteFrame = this.spriteList_0[0];
        }


        for(let i = 0 ; i < num ; i++){
            if(i != 0){
                let item = cc.instantiate(this.btnItem);
                item.parent = content_1;
            }
        }

        for(let i = 0 ; i < num ; i++){
            this.addButtonEvent(content_1.children[i],i);
            let lab = content_1.children[i].getChildByName("lab");
            lab.getComponent(cc.Label).string = this.labStringList.layer_1[i];
            let pic = content_1.children[i].getChildByName("pic");
            pic.getComponent(cc.Sprite).spriteFrame = this.spriteList_1[i];

            let lock = content_1.children[i].getChildByName("lock");
            let lock_pic = content_1.children[i].getChildByName("lock_pic");
            lock.getComponent(cc.Sprite).spriteFrame = this.lock;
            lock_pic.getComponent(cc.Sprite).spriteFrame = this.lockList[0];

            let scene = sceneMediator.getShowScene() > sceneMediator.getHighScene() ? sceneMediator.getShowScene() : sceneMediator.getHighScene();
            if(scene < 13){
                if(i == 0 || i == num - 1){
                    lock.active = false;
                    lock_pic.active = false;
                }else{
                    lock.active = true;
                    lock_pic.active = true;
                }
            }else if(scene >= 13 && scene < 31){
                if(i < 2 || i == num - 1){
                    lock.active = false;
                    lock_pic.active = false;
                }else{
                    lock.active = true;
                    lock_pic.active = true;
                }
            }else if(scene >= 31){
                lock.active = false;
                lock_pic.active = false;
            }

            if(i == 3){
                lock.active = true;
                lock_pic.active = true;
            }
        }

    }

    addButtonEvent(node,index){
        let button = node.addComponent(cc.Button);
        let clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node;
        clickEventHandler.component = "chooseLevelRank";
        clickEventHandler.handler = "labBtnClickEvent";
        clickEventHandler.customEventData = `${index}`;
        button.transition = cc.Button.Transition.SCALE;
        button.clickEvents.push(clickEventHandler);
    }

    labBtnClickEvent(event,data){
        if(parseInt(data) < 3){
            let scene = sceneMediator.getShowScene() > sceneMediator.getHighScene() ? sceneMediator.getShowScene() : sceneMediator.getHighScene();
            if(scene < this.levelData[1] - 1 && (parseInt(data) == 1 || parseInt(data) == 2)){
                return;
            }else if((scene >= this.levelData[1] - 1 && scene < this.levelData[2] - 1) && parseInt(data) == 2){
                return;
            }

            Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                let view = cc.instantiate(res);
                view.getComponent("levelViewMediator").setCallBack(parseInt(data),()=>{

                });
                view.parent = this.node;
            });

        }else if(parseInt(data) == 3){
            return;
            Tool.loadPrefab("mainPrefab/princeLevelView",(res)=>{
                let view = cc.instantiate(res);
                view.getComponent("princeLevelViewMediator").setCallBack(()=>{

                });
                view.parent = this.node;
            });
        }else{

        }
    }

    backHome(){
        this.node.destroy();
    }


    setCallBack(callback){
        callback();
    }
}
